﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dqs_DAL
{
	public class Course_DAL
	{
		/// <summary>
		/// 插入课程信息
		/// </summary>
		/// <param name="courses"></param>
		/// <returns></returns>
		public  bool InsertCourseInfo(CourseInfo courses)
		{
			using (MyDbContext db = new MyDbContext())
			{
				if (courses == null)
					return false;
				db.CourseInfos.Add(courses);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id修改课程信息
		/// </summary>
		/// <param name="courseNew"></param>
		public  bool UpdateCourseInfoById(CourseInfo courseNew)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var courseOld = db.CourseInfos.Where(x => x.Id == courseNew.Id).FirstOrDefault();
				if (courseOld == null)
				{
					return false;
				}
				courseOld.CourseName = courseNew.CourseName;
				courseOld.Teachers = courseNew.Teachers;
				courseOld.StudentList = courseNew.StudentList;
				courseOld.Location = courseNew.Location;
				courseOld.StartTime = courseNew.StartTime;
				courseOld.EndTime = courseNew.EndTime;
				courseOld.Description = courseNew.Description;
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id删除课程信息
		/// </summary>
		/// <param name="courseId"></param>
		/// <returns></returns>
		public bool DeleteCourseInfoById(int courseId)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var course = db.CourseInfos.FirstOrDefault(s => s.Id == courseId);
				if (course == null)
				{
					return false;
				}
				db.CourseInfos.Remove(course);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 查询所有课程信息
		/// </summary>
		/// <returns></returns>
		public List<CourseInfo> QueryCourseInfo()
		{
			using (MyDbContext db = new MyDbContext())
			{
				var courses = db.CourseInfos.ToList();
				return courses;
			}
		}
	}
}
